<?php

/**
 * message actions.
 *
 * @package    wills
 * @subpackage message
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class messageActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->form = new MessageForm();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->message = Doctrine::getTable('Message')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->message);
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->messages = Doctrine::getTable('Message')
      ->createQuery('a')
      ->execute();
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new MessageForm();

    $this->processForm($request, $this->form);
    $this->setTemplate('index');
    
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $message = $form->save();
      $this->getUser()->setFlash('message', '恭喜您，发表留言成功！');
      $this->redirect('message');
    }
  }
}
